#!/bin/bash

DESC="uShare UPnP A/V Media Server"

. /etc/rc.conf
. /etc/rc.d/functions
[ -r "/etc/ushare/ushare.conf" ] && . /etc/ushare/ushare.conf
[ -r "/etc/conf.d/ushare" ] && . /etc/conf.d/ushare

[ -z $USHARE_USER ] && USHARE_USER=ushare

# abort if no executable exists
#[ -x $DAEMON ] || stat_fail

# abort if no shared directory is defined
#[ -z "$USHARE_DIR" ] && stat_fail

#set -e

#checkpid() {
#  [ -e $PIDFILE ] || touch $PIDFILE
#}

PID=`pidof -o %PPID /usr/bin/ushare`
case "$1" in
  start)
    stat_busy "Starting $DESC"
    [ -z "$PID" ] && sudo -u $USHARE_USER /usr/bin/ushare -D -x $PARAMS
    if [ $? -gt 0 ]; then
      stat_fail
    else
      PID=`pidof -o %PPID /usr/bin/ushare`
      echo $PID > /var/run/ushare.pid
      add_daemon ushare
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping $DESC"
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm /var/run/ushare.pid
      rm_daemon ushare
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"
esac
